#include "Precompile.h"
#include "AnimationManager.h"

namespace Graphics{
	AnimationManager::AnimationManager():
		m_animControllerId(0),m_animClipId(0),m_animSamplerId(0){
	}

	void AnimationManager::update(float dt, RenderContext* context){
		std::set<MeshPtr>::iterator itr = m_meshes.begin();
		while(itr != m_meshes.end()){
			(*itr)->updateAnimation(dt,context);
			itr++;
		}
	}

	unsigned int AnimationManager::addAnimationController(AnimationControllerPtr controller){
		unsigned int id = m_animControllerId++;
		m_animationControllers[id] = controller;
		return id;
	}

	unsigned int AnimationManager::addAnimationClip(AnimationClipPtr clip){
		unsigned int id = m_animClipId++;
		m_animationClips[id] = clip;
		return id;
	}

	unsigned int AnimationManager::addAnimationSampler(AnimationSamplerPtr sampler){
		unsigned int id = m_animSamplerId++;
		m_animationSamplers[id] = sampler;
		return id;
	}

	AnimationControllerPtr AnimationManager::getAnimationController(unsigned int id){
		std::map<unsigned int,AnimationControllerPtr>::iterator itr = m_animationControllers.find(id);
		if(itr == m_animationControllers.end()){
			LOG_ERROR("Controller does not exist.");
			assert(0);
			return AnimationControllerPtr();
		}

		return itr->second;
	}

	AnimationClipPtr AnimationManager::getAnimationClip(unsigned int id){
		std::map<unsigned int,AnimationClipPtr>::iterator itr = m_animationClips.find(id);
		if(itr == m_animationClips.end()){
			LOG_ERROR("Clip does not exist.");
			assert(0);
			return AnimationClipPtr();
		}

		return itr->second;
	}

	AnimationSamplerPtr AnimationManager::getAnimationSampler(unsigned int id){
		std::map<unsigned int,AnimationSamplerPtr>::iterator itr = m_animationSamplers.find(id);
		if(itr == m_animationSamplers.end()){
			LOG_ERROR("Sampler does not exist.");
			assert(0);
			return AnimationSamplerPtr();
		}

		return itr->second;
	}

	void AnimationManager::addAnimatedMeshPtr(MeshPtr mesh){
		m_meshes.insert(mesh);
	}

	void AnimationManager::removeAnimatedMeshPtr(MeshPtr mesh){
		m_meshes.erase(mesh);
	}
}